Converting models from Python to TensorFlowJS

Directly from Coursera DeepLearning.ai course.

pip install tensorflowjs
import numpy as np
import tensorflow as tf

print('\u2022 Using TensorFlow Version:', tf.__version__)
model = tf.keras.models.Sequential([
        tf.keras.layers.Dense(units=1, input_shape=[1])  
])

model.compile(optimizer='sgd', loss='mean_squared_error')

xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)

model.fit(xs, ys, epochs=500)

Exporting the model

import time
saved_model_path = "./{}.h5".format(int(time.time()))

model.save(saved_model_path)

Converting the model to JSON and weights bin files.

tensorflowjs_converter --input_format=keras {saved_model_path} ./